package cn.kgc.crowdfundingadmin.controller;

import cn.kgc.crowdfundingadmin.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadmin.common.ResultEntity;
import cn.kgc.crowdfundingadmin.pojo.Admin;
import cn.kgc.crowdfundingadmin.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-02-19 15:15
 */
@Controller
public class AdminController {
    @Resource
    AdminService adminService;

    @RequestMapping("/admin/do/login")
    public String doLogin(String loginAcct, String userPswd, HttpSession session, Model model){
        Admin admin=adminService.login(loginAcct,userPswd);
        if(admin!=null){
            session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN,admin);
            /*return "admin-main";*/
            return "redirect:/admin/to/main/page.html";
        }else{
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE,CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "admin-login";
        }
   }
//分页查询
@RequestMapping("/admin/query/for/search")
public String queryForSearch(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "5",required = false)Integer pageSize,
                             @RequestParam(value = "keyword",defaultValue = "",required = false)String keyword,Model model
){
    PageInfo<Admin> pageInfo=adminService.queryForKeywordSearch(pageNum,pageSize,keyword);
    model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO,pageInfo);
    System.out.println(pageInfo.toString());
    return "admin-page";
}
//退出
    @RequestMapping("/admin/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index.html";
    }
    //异常处理
    @ExceptionHandler(value=Exception.class)
    public String catchException(Exception exception,Model model) {
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE, exception);
        return "system-error";
    }
    //删除
    @RequestMapping("/admin/batch/remove")
    @ResponseBody
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdlist){
        adminService.remove(adminIdlist);
        return ResultEntity.successWithoutData();
    }
    //新增
    @RequestMapping("/admin/save")
    public String save(Admin admin){
        adminService.saveAdmin(admin);
        return "redirect:/admin/query/for/search";
    }
    //修改(1)
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(Integer adminId,Integer pageNum, Model model) {

        Admin admin = adminService.getAdminById(adminId);

        model.addAttribute(CrowdFundingConstant.ATTR_ADMIN, admin);

        return "admin-edit";
    }
    //修改(2)
    @RequestMapping("/admin/update")
    public String edit(Integer pageNum,Admin admin){

        try {
            adminService.updateAdmin(admin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/admin/query/for/search?pageNum"+pageNum;
    }
}
